<template>
  <div class="datamap">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>文章数量分类统计</span>
          </div>
          <div id="category"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>日发布文章数量统计</span>
          </div>
          <div id="publishitime"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'HomePage',
  data() {
    return {
      articleNumByC: [],
      articleNumByP: {
        time: [],
        num: []
      }
    }
  },
  mounted() {
    this.getArticleNum()
  },
  methods: {
    initChar() {
      const publishitimeOption = {
        tooltip: {},
        xAxis: {
          // type: "sort",
          // config: { dimension: this.articleNumByP.time, order: "desc" },
          axisLabel: { interval: 0, rotate: 30 },
          data: this.articleNumByP.time
        },
        yAxis: {},
        series: {
          type: 'bar',
          // encode: { x: "name", y: "score" },
          data: this.articleNumByP.num
        }
      }
      const categoryOption = {
        tooltip: {},
        // legend:{
        //   y:'bottom'
        // },
        series: {
          // name: "我是饼图",
          type: 'pie',
          radius: '55%',
          data: this.articleNumByC
        }
      }
      const myChartTwo = this.$echarts.init(document.getElementById('publishitime')) //图标初始化
      myChartTwo.setOption(publishitimeOption)
      const myChartOne = this.$echarts.init(document.getElementById('category')) //图标初始化
      myChartOne.setOption(categoryOption)
      // window.onresize = function () {
      //   myChartTwo.resize();
      // };
    },
    getArticleNum() {
      this.$http.get('/echarts').then(res => {
        this.articleNumByC = res.result.totalC
        res.result.totalP.forEach(item => {
          const time = item.create_time.slice(0,10)
          this.articleNumByP.time.push(time)
          this.articleNumByP.num.push(item.totalP)
        })
        this.initChar()
      })
    }
  }
}

</script>

<style scoped>
.datamap #category,
.datamap #publishitime {
  height: 300px;
}
</style>
